package com.qianlou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianlou.common.enums.ResponseStatusEnum;
import com.qianlou.common.exception.Display;
import com.qianlou.mapper.InterviewerMapper;
import com.qianlou.pojo.Interviewer;
import com.qianlou.pojo.bo.InterviewerBO;
import com.qianlou.service.InterviewerService;
import com.qianlou.service.JobService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 数字人面试官表 服务实现类
 * </p>
 *
 * @author James
 * @since 2025-04-26
 */
@Service
public class InterviewerServiceImpl extends ServiceImpl<InterviewerMapper, Interviewer> implements InterviewerService {

    @Resource
    private InterviewerMapper interviewerMapper;

    @Resource
    private JobService jobService;

    @Override
    public void createOrUpdate(InterviewerBO bo) {
        Interviewer interviewer = new Interviewer();
        BeanUtils.copyProperties(bo, interviewer);
        interviewer.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(interviewer.getId())) {
            interviewer.setCreateTime(LocalDateTime.now());
            interviewerMapper.insert(interviewer);
        } else {
            interviewerMapper.updateById(interviewer);
        }
    }

    @Override
    public List<Interviewer> selectAll() {
        List<Interviewer> interviewers = interviewerMapper.selectList(
                new QueryWrapper<Interviewer>().orderByAsc("updated_time"));
        return interviewers;
    }

    @Override
    public void deleteById(String id) {
        Long jobCountInInterviewer = jobService.getJobCountInInterviewer(id);
        if (jobCountInInterviewer != null && jobCountInInterviewer > 0) {
            Display.throwException(ResponseStatusEnum.CAN_NOT_DELETE_INTERVIEWER);
        }
        interviewerMapper.deleteById(id);
    }
}
